package com.runchinaup.smartbelt.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.runchinaup.runchilibrary.modes.adapter.ListBaseAdapter;
import com.runchinaup.smartbelt.R;
import com.runchinaup.smartbelt.domian.AlarmCanData;
import com.runchinaup.smartbelt.domian.AlarmData;
import com.runchinaup.smartbelt.preferences.AlarmCanInfoPre;
import com.suke.widget.SwitchButton;

import java.util.List;

public abstract class AlarmAdapter extends ListBaseAdapter<AlarmData, AlarmAdapter.ThisTag> {

    public AlarmAdapter(Context context, List<AlarmData> datas) {
        super(context, datas);
    }

    @Override
    protected int loaditemView() {
        return R.layout.item_alarm;
    }

    @Override
    protected ThisTag instanceTag(View convertView) {
        return new ThisTag(convertView);
    }

    @SuppressLint("ResourceType")
    @Override
    protected void handDataAndView(final ThisTag thisTag, final AlarmData alarmData, final int position) {

        byte[] weekData = alarmData.everyDay();

        for (int i = 0; i < 7; i++) {
            if (weekData[i] == 1) {
                thisTag.week[i].setChecked(true);
            } else {
                thisTag.week[i].setChecked(false);
            }
        }

        AlarmCanData alarmCanData = AlarmCanInfoPre.read(context);
        boolean openClose = false;
        if (alarmCanData != null) {
            switch (position) {
                case 0:
                    openClose = alarmCanData.isOneAlarm();
                    break;
                case 1:
                    openClose = alarmCanData.isTwoAlarm();
                    break;
                case 2:
                    openClose = alarmCanData.isThreeAlarm();
                    break;
            }
        }

        thisTag.alarmNumber.setText(String.format(context.getResources().getString(R.string.alarm_text), (position + 1)));
        thisTag.alarmTime.setText(String.format("%02d:%02d", alarmData.getAlarmHour(), alarmData.getAlarmMintue()));
        thisTag.switchButton.setChecked(openClose);

        thisTag.alarmTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChangeTime(alarmData, thisTag.alarmTime, thisTag.switchButton, position);
            }
        });
        thisTag.switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton switchButton, boolean isChecked) {
                onSwitchButton(switchButton, isChecked, position);
            }
        });

        CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                weekDataCallback(alarmData.getWeekResult(thisTag.week), position);
            }
        };

        for (CheckBox checkBox : thisTag.week) {
            checkBox.setOnCheckedChangeListener(checkedChangeListener);
        }

    }

    protected abstract void onChangeTime(AlarmData alarmData, TextView alarmTime, SwitchButton switchButton, int position);

    protected abstract void onSwitchButton(SwitchButton switchButton, boolean isChecked, int position);

    protected abstract void weekDataCallback(int weekCycle, int position);


    public static class ThisTag extends ListBaseAdapter.Tag {

        public TextView alarmNumber;
        public TextView alarmTime;
        public SwitchButton switchButton;
        public CheckBox[] week = new CheckBox[7];


        public ThisTag(View view) {
            super(view);
            alarmNumber = $View(R.id.alarm_number);
            alarmTime = $View(R.id.alarm_time);
            switchButton = $View(R.id.btn_switch);
            week[0] = $View(R.id.Monday);
            week[1] = $View(R.id.Tuesday);
            week[2] = $View(R.id.Wednesday);
            week[3] = $View(R.id.Thursday);
            week[4] = $View(R.id.Friday);
            week[5] = $View(R.id.Saturday);
            week[6] = $View(R.id.Sunday);
        }
    }
}
